﻿#include "stdafx.h"
#include "UserInit.h"
#include "tlhelp32.h"
#include <winsock2.h> 


CIniFile g_IniFile;
CIniFile g_AccountFile;
CString arrState[] = { L"未完成",L"完成",L"账号错误",L"二密错误",L"武器未装备",L"账号在线",L"Boss已完成" };
bool CheckState = false;
bool g_flag=true;//全局状态
bool g_bloodState = false;
int g_nState = 0;
bool g_bSwicth = false;
int g_nBossX = 0;
bool g_BossState = false;
map<CString, OcrInfo>ocrMap;
CString arrLineCode[20];
int g_Point[8][2] = {
	{ 133,214 },
	{ 259,214 },
	{ 374,210 },
	{ 499,219 },
	{ 133,411 },
	{ 259,412 },
	{ 384,407 },
	{ 505,412 },

};
int SplitVARIANT(CString source, VARIANT* nlist, CString divKey) {

	int j = 0; int Number = 0; int m_pos, d_len = divKey.GetLength();
	while (TRUE)
	{
		m_pos = source.Find(divKey, j);
		if (m_pos == -1 && j == 0) { break; }
		if (m_pos == -1 && j != 0)
		{

			nlist[Number].lVal = _wtof(source.Mid(j, source.GetLength() - j));
			nlist[Number].lVal = nlist[Number].lVal < 0 ? 0 : nlist[Number].lVal;
			break;
		}
		if (j == 0)
		{
			nlist[Number].lVal = _wtof(source.Mid(0, m_pos));
			nlist[Number].lVal = nlist[Number].lVal < 0 ? 0 : nlist[Number].lVal;
		}
		else {
			nlist[Number].lVal = _wtof(source.Mid(j, m_pos - j));
			nlist[Number].lVal = nlist[Number].lVal < 0 ? 0 : nlist[Number].lVal;
		}
		j = m_pos + d_len;
		Number++;
	}
	return Number;
}


CString NumberToString(LONG Number)
{
	CString sTring;
	sTring.Format(_T("%ld"), Number);
	return sTring;
}


DWORD RandomNumber(DWORD dwNumber1, DWORD dwNumber2)
{
	DWORD dwTemp;
	__asm
	{
		push ecx
		push edx
		rdtsc
		mov ecx, 0x17
		mul ecx
		add eax, 0x7
		mov ecx, dwNumber2//ss:[ebp + 0c]
		sub ecx, dwNumber1//ss : [ebp + 8]
		inc ecx
		xor edx, edx
		div ecx
		add edx, dwNumber1//ss : [ebp + 8]
		mov dwTemp, edx
		pop edx
		pop ecx
	}
	return dwTemp;
}
//分割字符串
void StringSplit(CString source, CStringArray& dest, char division)
{
	if (source.IsEmpty())
		return;
	dest.RemoveAll();
	for (int i = 0; i < source.GetLength(); i++)
	{
		if (source.GetAt(i) == division)
		{
			dest.Add(source.Left(i));
			source.Delete(0, i + 1);
			i = 0;
		}
	}
	dest.Add(source);
}

void KillProcess(CString sExeName)
{
	HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapShot == 0)
		return;
	PROCESSENTRY32 thePE;
	thePE.dwSize = sizeof(PROCESSENTRY32);


	//遍历正在运行的第一个系统进程


	bool Status = Process32First(hSnapShot, &thePE);
	bool bHaveFlag = false;
	DWORD ProcessID = 0;


	while (Status)
	{
		//遍历正在运行的下一个系统进程  
		Status = Process32Next(hSnapShot, &thePE);
		//找到相应的进程 **.exe


		// if(0 == wcscmp(thePE.szExeFile,L""))
		CString sFindName = thePE.szExeFile;
		CString sTemp = sExeName.Mid(0, sFindName.GetLength());


		if (sFindName == sTemp)
		{
			bHaveFlag = true;
			ProcessID = thePE.th32ProcessID;


			////结束指定的进程 ProcessID
			if (!TerminateProcess(OpenProcess(PROCESS_TERMINATE || PROCESS_QUERY_INFORMATION, false, ProcessID), 0))
			{
				//AfxMessageBox(L"无法终止指定的进程！", MB_ICONWARNING || MB_OK);
			}
			else
			{
				//AfxMessageBox(L"进程结束！", MB_ICONWARNING || MB_OK);
				break;
			}
		}
	}
	CloseHandle(hSnapShot);
}


//函数功能:字符串拆分(待分割字符串,保存分割的数组,分隔符)[返回:个数]
int Split(CString source, CString* array, CString divKey)
{
	int j = 0; int Number = 0; int m_pos, d_len = divKey.GetLength();
	while (TRUE)
	{
		m_pos = source.Find(divKey, j);
		if (m_pos == -1 && j == 0) { break; }
		if (m_pos == -1 && j != 0)
		{
			array[Number] = source.Mid(j, source.GetLength() - j);
			break;
		}
		if (j == 0)
		{
			array[Number] = source.Mid(0, m_pos);
		}
		else {
			array[Number] = source.Mid(j, m_pos - j);
		}
		j = m_pos + d_len;
		Number++;
	}
	return Number;
}
//函数功能:字符串拆分(待分割字符串,保存分割的数组,分隔符)[返回:个数]
int Split2(CString source, CStringArray& dest, CString division)
{
	dest.RemoveAll();
	int nCount = 0;
	for (int i = 0; i<source.GetLength(); i++)
	{
		if (source.GetAt(i) == division)
		{
			nCount = i;
			dest.Add(source.Left(i));//去右边
			int nIndex = dest.GetSize() - 1;
			for (int j = 0; j<nIndex; j++)
			{
				dest[nIndex] = dest[nIndex].Right(dest[nIndex].GetLength() - dest[j].GetLength() - 1);//去左边
																									  //TRACE0(dest[nIndex] + "\n");
			}
		}
	}
	//分割符division右边的剩余部分
	dest.Add(source);
	int nIndex2 = dest.GetSize() - 1;
	for (int j = 0; j<nIndex2; j++)
	{
		dest[nIndex2] = dest[nIndex2].Right(dest[nIndex2].GetLength() - dest[j].GetLength() - 1);
		//TRACE0(dest[nIndex2] + L"\n");
	}
	return nCount;
}

//转char*(参数CString类型)[返回:char*]
const char* StrP(CString str)
{

	int nLength = str.GetLength();
	int nBytes = WideCharToMultiByte(CP_ACP, 0, str, nLength, NULL, 0, NULL, NULL);
	char* VoicePath = new char[nBytes + 1];
	memset(VoicePath, 0, nLength + 1);
	WideCharToMultiByte(CP_OEMCP, 0, str, nLength, VoicePath, nBytes, NULL, NULL);
	VoicePath[nBytes] = 0;
	return VoicePath;
}